package com.atguigu.springcloud.controller;

import cn.hutool.core.util.IdUtil;
import com.atguigu.springcloud.common.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName : com.atguigu.springcloud.controller.PaymentController
 * @Description : 类描述
 * Created by cyd on 2021-09-13 14:59:40
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${spring.application.name}")
    private String serviceName;

    @GetMapping("/feignTimeout")
    public String feignTimeout() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return serviceName + ":" + serverPort;
    }

    @GetMapping(value = "/discovery")
    public Object discovery() {
        // 发现服务
        List<String> serviceList = discoveryClient.getServices();
        serviceList.forEach(service -> {
            log.info("****** serviceName:" + service);
        });

        // 发现本服务下的每个实例
        List<ServiceInstance> instanceList = discoveryClient.getInstances(serviceName);
        instanceList.forEach(instance -> {
            log.info("serviceId:{}, host:{}, port:{}, uri:{}",
                    instance.getServiceId(), instance.getHost(),
                    instance.getPort(), instance.getUri());
        });

        // 发现微服务集群中，每一个服务下面的，所有的实例
        serviceList.forEach(service -> {
            List<ServiceInstance> instances = discoveryClient.getInstances(service);
            instances.forEach(instance -> {
                log.info("￥￥￥￥ serviceId:{}, host:{}, port:{}, uri:{}",
                        instance.getServiceId(), instance.getHost(),
                        instance.getPort(), instance.getUri());
            });
            log.info("下一个服务");
        });

        return discoveryClient;
    }

    @PostMapping(value = "/creat")
    public CommonResult<Integer> creatPayment(@RequestBody Payment payment) {
        int result = paymentService.creat(payment);
        log.info("数据库插入行数------{}", result);
        if (result > 0) {
            return new CommonResult<Integer>(200, "success,serverPort:" + serverPort, result);
        } else {
            return new CommonResult<Integer>(444, "fail,serverPort:" + serverPort, result);
        }
    }

    @GetMapping(value = "/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPayment(id);
        log.info("查询结果---{}", payment);
        if (null != payment) {
            return new CommonResult<Payment>(200, "success,serverPort:" + serverPort, payment);
        } else {
            return new CommonResult<>(444, "fail,serverPort:" + serverPort, null);
        }

    }

    @GetMapping(value = "/uuid/{id}")
    public CommonResult<Payment> getUuid(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPayment(id);
        String uuid = IdUtil.simpleUUID();
        String uuidSerial = payment.getSerial() + "---" + uuid;
        payment.setSerial(uuidSerial);
        log.info("查询结果---{}", payment);

        if (null != payment) {
            return new CommonResult<Payment>(200, "success,serverPort:" + serverPort, payment);
        } else {
            return new CommonResult<>(444, "fail,serverPort:" + serverPort, null);
        }
    }

    @GetMapping(value = "/lb")
    public CommonResult<Payment> getPort() {
        return new CommonResult<>(200, serverPort);
    }
}
